﻿using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

    private CharacterController m_cc;

    private Animator m_anim;

    public float speed = 5;

    
	// Use this for initialization
	void Start () {
        m_cc=GetComponent<CharacterController>();
	    m_anim=GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {
        // 获取水平方向  ws 键盘 慢慢返回 1 -1
        float shuiping = Input.GetAxis("Horizontal");
        // 获取垂直方向 ad 键盘 返回 1 -1
        float chuizhi = Input.GetAxis("Vertical");

        if (JoyStickController.h != 0 || JoyStickController.v != 0)
        {
            shuiping = JoyStickController.h;
            chuizhi = JoyStickController.v;
        }


        if (Mathf.Abs(shuiping) >= 0.1f || Mathf.Abs(chuizhi) >= 0.1f)
        {
            m_anim.SetBool("Run", true);

  
            // 得到当前动画信息  名字
            if (m_anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerRun") || m_anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerStand"))
            {

                Vector3 targetDir = new Vector3(shuiping, 0, chuizhi);

                transform.LookAt(targetDir + transform.position);

                m_cc.SimpleMove(targetDir * speed);
                
            }

        }
        else
        {
            m_anim.SetBool("Run",false);
 
        }



	}
}
